package msu.ml.data.level2;

import java.io.*;
import java.util.zip.*;

/**
 * BlockReader reads blocks of nexrad level II data from an
 * archive II file.
 *
 * BlockReader <a href="http://crane.cs.montana.edu/cgi-bin/cvsweb.cgi/mltoolkit/src/msu/ml/data/level2/BlockReader.java">[CVS]</a>
 * reads packets of data from an archive II file. Packets either contain 
 * radial radar data or messages from the radar system. BlockReader can read
 * both Build 9 (.Z) and Build 10 (.gz) packets.
 *
 * @author Reginald M Mead
 * @version 1.0
 * @see IPacket
 */
public class BlockReader
{
	protected Level2InputStream istream;
	protected Header header = null;
   private int build;

	/** 
	 * Create a new BlockReader for the specified file
    *
    * BlockReader reads header information from the file
    * during initialization.
	 *
	 * @param file file to read
	 */
	public BlockReader(String file)
	{
		try
		{
         if(file.endsWith(".Z"))
         {
            this.build = 9;
            this.istream = new Level2InputStream(new UncompressInputStream(
                  new FileInputStream(file)));

            byte [] buffer = new byte[9];
            this.istream.readBytes(buffer, 9);
            String fileName = new String(buffer);
            if(fileName.equals("ARCHIVE2.") || fileName.equals("AR2V0001."))
            {
               header = new Header();
               System.arraycopy(buffer, 0, header.name, 0, 9);
               this.istream.readBytes(header.ext, 0, 3);
               header.date = this.istream.readInt();
               header.time = this.istream.readInt();
               this.istream.skip(4);
            }
         }
         else if(file.endsWith(".gz"))
         {
            this.build = 10;
            this.istream = new Level2InputStream(new GZIPInputStream(
                  new FileInputStream(file)));

            byte [] buffer = new byte[12];
            this.istream.readBytes(buffer, 12);
            String fileName = new String(buffer);
            if(fileName.startsWith("AR2V0003."))
            {
               header = new Header(12);
               System.arraycopy(buffer, 0, header.name, 0, 12);
               this.istream.skip(2);
               header.date = this.istream.readShort();
               header.time = this.istream.readInt();
               this.istream.readBytes(header.call, 4);
            }
         }
         else
         {
            this.build = 10;
            this.istream = new Level2InputStream(new FileInputStream(file));

            byte [] buffer = new byte[12];
            this.istream.readBytes(buffer, 12);
            String fileName = new String(buffer);
            if(fileName.startsWith("AR2V0003."))
            {
               header = new Header(12);
               System.arraycopy(buffer, 0, header.name, 0, 12);
               this.istream.skip(2);
               header.date = this.istream.readShort();
               header.time = this.istream.readInt();
               this.istream.readBytes(header.call, 4);
            }
         }

		}
		catch(IOException e)
		{
         System.out.println("BlockReader Error: " + e.getMessage());
         e.printStackTrace();
		}
	}

	/**
	 *	Read the next data Packet
	 *
	 *	@return the next Packet of data
    * @see IPacket
	 */
	public IPacket read()
	{
      counter++;
      if(this.build == 10)
         return new Build10Packet(this.istream);
      else
         return new Build9Packet(this.istream);
	}

   public int counter = 0;

	/**
	 * Return the header information for the archive II file.
	 *
	 * @return Header the header for this file
	 */
	public Header getHeader()
	{
		return header;
	}


	/**
	 * Close the data stream
	 */
	public void close()
	{
      istream.close();
	}

	/**
	 * Close the data stream
	 */
	protected void finalize() throws Throwable 
	{
		try 
		{
			close();        // close open files
		} 
		finally 
		{
			super.finalize();
		}
	}
}

